package com.ttai.presenter.activity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.ttai.model.dao.DBHelper;
import com.ttai.model.net.ResponseInfo;
import com.ttai.model.net.TokenInfo;
import com.ttai.presenter.base.BasePresenter;
import com.ttai.ui.activity.Register;
import com.ttai.ui.activity.UserHomePage;

import retrofit2.Call;

public class SigninPresenter extends BasePresenter {
    Register register;

    public SigninPresenter(Register register) {
        this.register = register;

    }



    @Override
    protected void showError(String message) {
        Toast.makeText(register,message,Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void parseJson(JsonObject json) {
        Toast.makeText(register,"注册成功",Toast.LENGTH_SHORT).show();
        Gson gson = new Gson();
        TokenInfo tokenInfo = gson.fromJson(json,TokenInfo.class);
        insertToken(tokenInfo.getToken());

        register.finish();
        Intent intent=new Intent();
        intent.setClass(register,UserHomePage.class);
        register.startActivity(intent);
    }

    private void insertToken(String token) {
        DBHelper dbHelper = DBHelper.getInstance(register);
        SQLiteDatabase db=dbHelper.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put("token",token);
        db.update("tokenacount",values, null, null);
    }

    public void register(String scode,String phone,String email,String cid){
        Call<ResponseInfo> register = responseInfoApi.register(scode, phone, email, cid);
        register.enqueue(new CallBackAdapter());
    }

    public void encrypt(String token,String accountName,String account,String password){
        Call<ResponseInfo> acountAdd = responseInfoApi.acountadd(token, accountName, account, password);
        acountAdd.enqueue(new CallBackAdapter());
    }
}
